<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加</title>
</head>
<body>
<div>
    <table border="1px">
        <thead>
        <tr><h2>添加健身卡信息</h2></tr>
        </thead>
        <tbody>
        <tr>
            <td>持卡人姓名(*)</td>
            <td><input type="text" id="userName"></td>
        </tr>
        <tr>
            <td>手机号(*)</td>
            <td><input type="text" id="phone"></td>
        </tr>
        <tr>
            <td>卡类型(*)</td>
            <td>
                <select id="select">

                </select>
            </td>
        </tr>
        <tr>
            <td>卡内余额(*)</td>
            <td><input type="text" id="balance"></td>
        </tr>
        <tr>
            <td>开卡日期(*)</td>
            yyyy-MM-dd
            <td><input type="text" id="openDate"></td>
        </tr>

        </tbody>
        <p><input id="submit" type="submit" value="提交"/></p>
    </table>

</div>
</body>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
    $(function () {
        $.ajax({
            "url": '/type/list/type',
            "type": "post",
            "dataType": "json",
            "success": function (a) {
                let html = "<option value='0'>---请选择---</option>";
                for (var i = 0; i < a.length; i++) {
                    html += "  <option value='" + (i + 1) + "'>" + a[i].name + "</option>";
                }
                $('#select').append(html);
            },
            error: function () {
                alert("加载失败");
                // window.location.href = "./index.html";
            }
        });
        $('#submit').click(function () {
            let userName = $('#userName').val();
            let phone = $('#phone').val();
            let cardTypeId = $('select option:selected').val();
            let balance = $('#balance').val();

            let openDate = document.getElementById("openDate").value;
            // let openDate = $('#openDate').val();
            //var regExp = /^(\d{4})-(\d{2})-(\d{2})$/
            let result = openDate.match(/((^((1[8-9]\d{2})|([2-9]\d{3}))(-)(10|12|0?[13578])(-)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(11|0?[469])(-)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(0?2)(-)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)(-)(0?2)(-)(29)$)|(^([3579][26]00)(-)(0?2)(-)(29)$)|(^([1][89][0][48])(-)(0?2)(-)(29)$)|(^([2-9][0-9][0][48])(-)(0?2)(-)(29)$)|(^([1][89][2468][048])(-)(0?2)(-)(29)$)|(^([2-9][0-9][2468][048])(-)(0?2)(-)(29)$)|(^([1][89][13579][26])(-)(0?2)(-)(29)$)|(^([2-9][0-9][13579][26])(-)(0?2)(-)(29)$))/);

            if (cardTypeId == 0 || userName == '' || phone == '' || balance == '' || openDate == '') {
                alert("请输入完整的信息")
                return;
            } else if (result == null) {
                alert("日期格式不正确!")
                return false;
            }else{
                $.ajax({
                    "url": '/card/insert',
                    "type": "put",
                    "contentType":"application/json",
                    "dataType": "text",
                    "data":{
                        "userName":userName,
                        "cardTypeId":cardTypeId,
                        "phone":phone,
                        "balance":balance,
                        "openDate":openDate
                    },
                    "success": function (response) {
                       if(response){
                           alert("办卡成功")
                       }else{
                           alert("办卡失败")
                       }
                    },
                    error: function () {
                        alert("办卡失败");
                        // window.location.href = "./index.html";
                    }
                });
            }

        })

    });
</script>
</html>